package com.lfz.quality_effect.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @Auther: Destiny
 * @Date: 2020/8/5 16:45
 * @Description:
 */
public class TimeUtils {
    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    public static final String YYYYMM = "yyyy-MM";

    public TimeUtils() {
    }

    public static String getCurrentDate(String var1) {
        return (new SimpleDateFormat(var1)).format(new Date());
    }

    public static Date parse(String date) {
        if (StringUtils.isEmpty(date)) {
            return null;
        } else {
            try {
                return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(date);
            } catch (ParseException var2) {
                return null;
            }
        }
    }

    public static Date parse(String date, String format) {
        if (StringUtils.isEmpty(date)) {
            return null;
        } else {
            try {
                return (new SimpleDateFormat(format)).parse(date);
            } catch (ParseException var3) {
                return null;
            }
        }
    }

    public static Date parseToYMD(String date) {
        try {
            return (new SimpleDateFormat("yyyy-MM-dd")).parse(date);
        } catch (ParseException var2) {
            return null;
        }
    }

    public static String format(Date date) {
        return date != null ? (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date) : "/";
    }

    public static String format(Date date, String format) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static Date addYear(Date date, int year) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(1, year);
        date = calendar.getTime();
        return date;
    }

    public static Date addMonth(Date date, int month) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(2, month);
        date = calendar.getTime();
        return date;
    }

    public static Date addDay(Date date, int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, day);
        date = calendar.getTime();
        return date;
    }

    public static Date addHour(Date date, int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(10, day);
        date = calendar.getTime();
        return date;
    }

    public static Date addMinute(Date date, int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(12, day);
        date = calendar.getTime();
        return date;
    }

    public static Date addWeek(Date date, int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, day);
        date = calendar.getTime();
        return date;
    }

    public static Long getTimestamp() {
        return System.currentTimeMillis();
    }

    public static Map<String, String> getCurrentWeek(boolean addEndDay) {
        Map<String, String> map = new HashMap(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(2);
        int dayWeek = cal.get(7);
        if (dayWeek == 1) {
            dayWeek = 8;
        }

        cal.add(5, cal.getFirstDayOfWeek() - dayWeek);
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);
        map.put("beginWeek", weekBegin);
        cal.add(5, 4 + cal.getFirstDayOfWeek());
        if (addEndDay) {
            cal.add(5, 1);
        }

        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(addDay(sundayDate, -1)) + " 23:59:59";
        map.put("endWeek", weekEnd);
        return map;
    }

    public static Map<String, String> getCurrentMonth(boolean addEndDay) {
        Map<String, String> map = new HashMap(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        map.put("beginMonth", format(new Date(), "yyyy-MM") + "-01");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(5, calendar.getActualMaximum(5));
        if (addEndDay) {
            calendar.add(5, 1);
        }

        map.put("endMonth", sdf.format(calendar.getTime()));
        return map;
    }

    public static Map<String, String> getAppointMonth(Date date, boolean addEndDay) {
        Map<String, String> map = new HashMap(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        map.put("beginMonth", format(date, "yyyy-MM") + "-01");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(5, calendar.getActualMaximum(5));
        if (addEndDay) {
            calendar.add(5, 1);
        }

        map.put("endMonth", sdf.format(calendar.getTime()));
        return map;
    }

    public static String secondToTime(long second) {
        long days = second / 86400L;
        second %= 86400L;
        long hours = second / 3600L;
        second %= 3600L;
        long minutes = second / 60L;
        second %= 60L;
        return 0L < days ? days + "天" + hours + "小时" + minutes + "分" + second + "秒" : hours + "小时" + minutes + "分" + second + "秒";
    }

    public static int getYear(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
        return Integer.valueOf(sdf.format(date));
    }

    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }
    /**
     * 利用easyexcel解析出来的时间为43388
     *
     * @param time
     * @return
     */
    public static Date vertifyTime(Object time) {
        if (checkNumber(time.toString())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = sdf.parse(toDate(time.toString()));
                return parse;
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            Date date = checkTime(time.toString());
            return date;
        }
    }

    public static String strToDateFormat(String date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        formatter.setLenient(false);
        Date newDate= formatter.parse(date);
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(newDate);
    }
    public static String toDate(String value) {
        String regex = "\\d{8}";
        if (Pattern.matches(regex, value)){
            try {
                return strToDateFormat(value);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Calendar c = new GregorianCalendar(1900, 0, -1);
        value = value.split("\\.")[0];
        Date d = c.getTime();
        //42605是距离1900年1月1日的天数
        Date _d = DateUtils.addDays(d, Integer.parseInt(value));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(_d);
    }
    /**
     * 验证excel日期格式
     *
     * @param value
     * @return
     */
    public static Date checkTime(String value) {
        Date date = null;
        String[] possibleDateFormats = {
                "yyyy-MM-dd" ,
                "yyyyMMdd" ,
                "yyyy/MM/dd" ,
                "yyyy年MM月dd日" ,
                "yyyy.MM.dd",
        "yyyy-MM-dd HH:mm:ss"};

        try {
            date = DateUtils.parseDate(value, possibleDateFormats);
        } catch (ParseException e) {
            return date;
        }
        return date;
    }


    /**
     * 数字校验
     *
     * @param value
     * @return
     */
    public static boolean checkNumber(String value) {
        String regex = "^(-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9])|(-?[0])|(-?[0]\\.\\d*)$";
        return value.matches(regex);
    }


    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, year);
        calendar.roll(6, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }
}
